package com.icss.controller;

import com.icss.common.GroupA;
import com.icss.common.GroupB;
import com.icss.common.ServiceException;
import com.icss.pojo.Data0;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;

@Controller
public class HellController {

    /**
     * requestmapping("请求地址")
     * responsebody
     * @return
     */
    @GetMapping("/hello")
//    @RequestMapping(value="/hello", method = RequestMethod.GET)
    @ResponseBody
    public HashMap hello(@RequestParam("name") String p_name) {

        HashMap hm = new HashMap();
        hm.put("code", 20000);
        hm.put("data", p_name);
        hm.put("message", "hello world!");
        return hm;
    }

    @PostMapping("/hello")
    @ResponseBody
    public HashMap hello(@RequestBody HashMap param) {

        HashMap hm = new HashMap();
        hm.put("code", 20000);
        hm.put("data", param.get("name"));
        hm.put("message", "hello world!");

        return hm;
    }


    @PutMapping("/hello")
    @ResponseBody
    public HashMap hello(@RequestBody @Validated({GroupB.class}) Data0 param, BindingResult result) throws ServiceException {

        if (result.hasErrors()) {
            for (int i = 0; i < result.getFieldErrors().size(); i++) {
                FieldError item = result.getFieldErrors().get(i);
                throw new ServiceException(item.getDefaultMessage());
            }
        }

        HashMap hm = new HashMap();
        hm.put("code", 20000);
        hm.put("data", param);
        hm.put("message", "hello world!");

        return hm;
    }

    //     @DeleteMapping("/hello/{id}")
    @DeleteMapping("{id}/hello")
    @ResponseBody
    public HashMap hello(@PathVariable("id") int id, HttpServletRequest request) throws ServiceException {
        HashMap hm = new HashMap();
        hm.put("code", 20000);
        hm.put("data", id);
        hm.put("message", "hello world!");

        if (id < 100) {
            throw new ServiceException("id不能小于100");
        }
        System.out.println(request.getRealPath("/"));
        return hm;
    }



    @PutMapping("/hello1")
    @ResponseBody
    public HashMap hello1(@RequestBody @Validated({GroupA.class}) Data0 param, BindingResult result) throws ServiceException {

        if (result.hasErrors()) {
            for (int i = 0; i < result.getFieldErrors().size(); i++) {
                FieldError item = result.getFieldErrors().get(i);
                throw new ServiceException(item.getDefaultMessage());
            }
        }

        HashMap hm = new HashMap();
        hm.put("code", 20000);
        hm.put("data", param);
        hm.put("message", "hello world!");

        return hm;
    }

}
